<?php
/**
 * Created by PhpStorm.
 * User: JHR
 * Date: 2018/9/27
 * Time: 11:24
 */
namespace app\blog\controller;
use think\Db;
use think\exception\HttpResponseException;
use think\facade\Cache;
class Login extends Base {

    public function login() {
        return ajax('用户名密码不匹配',51);
    }

    public function checkToken() {
        $token = input('post.token');
        try {
            if(!$token) {
                return ajax('请传入token',-3);
            }else {
                $whereToken = [
                    ['token','=',$token]
                ];
                $token_exist = Db::table('mp_mix_token')->where($whereToken)->find();
                if(!$token_exist) {
                    return ajax('token无效',-3);
                }else {
                    if((time() - $token_exist['create_time']) > config('token_expires_time')) {
                        return ajax('token已过期',-3);
                    }
                }
            }
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

    public function getQrcode() {
        $key = randomkeys(6);
        $value = randomkeys(8);
        $cache = [
            'value' => $value,
            'login' => 0,
            'token' => ''
        ];
        Cache::set($key,$cache,120);
        $data = [
            'key' => $key,
            'value' => $value
        ];
        $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . "/blog/login/auth_user?".http_build_query($data);
        $data['url'] = $url;
        return ajax($data);
    }

    public function checkQrcodeState() {
        $key = input('post.key');
        $value = input('post.value');
        if($key) {
            $cache = Cache::get($key);
            if(!$cache) {
                return ajax('二维码已过期',52);
            }
            if($cache['value'] !== $value) {
                return ajax('缓存异常',62);
            }
            if($cache['login'] === 1) {
                Cache::rm($key);
            }
            return ajax($cache);
        }else {
            return ajax('',-1);
        }
    }

    public function auth_user(){
        if (!isset($_GET['code'])){ //没有code，去微信接口获取code码
            $callback = $_SERVER['REQUEST_SCHEME'] . '://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $this->get_code($callback);
        } else {    //获取code后跳转回来到这里了
            $code = $_GET['code'];
            $data = $this->get_access_token($code);//获取网页授权access_token和用户openid
            $data_all = $this->get_user_info($data['access_token'],$data['openid']);//获取微信用户信息
            $whereUser = [
                ['wx_openid','=',$data_all['openid']]
            ];
            $user_exist = Db::table('mp_mix_user')->where($whereUser)->find();
            $time = time();
            if($user_exist) {
                $uid = $user_exist['id'];
                $nickname = $data_all['nickname'];
                $avatar = $data_all['headimgurl'];
                $update_data = [
                    'nickname' => $nickname,
                    'avatar' => $avatar
                ];
                Db::table('mp_mix_user')->where($whereUser)->update($update_data);
            }else {
                $insert_data = [
                    'wx_openid' => $data_all['openid'],
                    'nickname' => $data_all['nickname'],
                    'avatar' => $data_all['headimgurl'],
                    'create_time' => $time
                ];
                $uid = Db::table('mp_mix_user')->insertGetId($insert_data);
            }
            $token = md5($time . $uid);
            $insert_data = [
                'uid' => $uid,
                'token' => $token,
                'create_time' => $time,
                'login_type' => 2
            ];
            Db::table('mp_mix_token')->insert($insert_data);
            $key = $_GET['key'];
            $value = $_GET['value'];
            if($key) {
                $cache = Cache::get($key);
                if(!$cache) {
                    die('二维码已过期');
                }
                if($cache['value'] !== $value) {
                    die('缓存异常');
                }
                $cache['token'] = $token;
                $cache['login'] = 1;
                Cache::set($key,$cache,6);
                $backurl = $_SERVER['REQUEST_SCHEME'] . '://'.$_SERVER['HTTP_HOST'] . '/static/loginsuccess.html?avatar=' . urlencode($data_all['headimgurl']);
                header('Location:'. $backurl);exit();
            }
            exit('login success');
        }

    }
    /**
     * 2、用户授权并获取code
     * @param string $callback 微信服务器回调链接url
     */
    private function get_code($callback,$scope = 'snsapi_userinfo'){
        $appid = config('appid');
        $state = md5(uniqid(rand(), true));//唯一ID标识符绝对不会重复
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($callback) .  '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
        header("Location:".$url);exit;
    }
    /**
     * 3、使用code换取access_token
     * @param string 用于换取access_token的code，微信提供
     * @return array access_token和用户openid数组
     */
    private function get_access_token($code){
        $appid = config('appid');
        $appsecret = config('app_secret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
            if($user->errcode == '40163') {
//                    echo 'Code been used!';exit;
                throw new HttpResponseException(ajax('Code been used!',-1));
            }else {
//                    echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
                throw new HttpResponseException(ajax($user->errmsg,-1));
            }
        }
        $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
        return $data;
    }
    /**
     * 4、使用access_token获取用户信息
     * @param string access_token
     * @param string 用户的openid
     * @return array 用户信息数组
     */
    private function get_user_info($access_token,$openid){
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
//            echo 'error:' . $user->errcode.'<hr>msg  :' . $user->errmsg;exit;
            throw new HttpResponseException(ajax($user->errmsg,49));
        }
        $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
        return $data;
    }

    public function getCode() {
        if(!isset($_GET['code'])) {
            $callback = $_SERVER['REQUEST_SCHEME'] . '://'.$_SERVER['HTTP_HOST'] . '/api/login/getCode';  //微信服务器回调url，这里是本页url
            $this->get_code($callback);
        }else {
            echo($_GET['code']);
        }
    }

    //高德地图获取地理位置
    private function getLocation() {
        $ip = $this->getip();
        $parameters = [
            'key' => '3a69bef99855dc5d7e80809e59111a4b',
            'type' => '4',
            'ip' => $ip,
        ];
        $url = 'https://restapi.amap.com/v5/ip?'.http_build_query($parameters);
        $res = curl_get_data($url);
        return json_decode($res,true);
    }

    protected function getip() {
        $unknown = 'unknown';
        if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown) ) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif ( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown) ) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        /*
        处理多层代理的情况
        或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
        */
        if (false !== strpos($ip, ',')) {
            $ip_arr = explode(',', $ip);
            $ip = reset($ip_arr);
        }
        return $ip;
    }



    protected function asyn_email_send($data) {
        $host = $_SERVER['HTTP_HOST'];
        $param = http_build_query($data);
        $fp = @fsockopen('ssl://' . $host, 443, $errno, $errstr, 20);
        if (!$fp){
            mylog($this->cmd,'error fsockopen','fsockopen.log');
        }else{
            stream_set_blocking($fp,0);
            $http = "GET /manage/email/accountWarning?".$param." HTTP/1.1\r\n";
            $http .= "Host: ". $host ."\r\n";
            $http .= "Connection: Close\r\n\r\n";
            fwrite($fp,$http);
            usleep(1000);
            fclose($fp);
        }
    }



}